﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;
using DotNetPerls;
using PMC;

namespace PMCgui
{
    public class StandardMenuStrip : MenuStrip, ITransitionable
    {
        protected ITransitionable context;
        //Menus
        protected ToolStripMenuItem File = new ToolStripMenuItem(Language.Instance.File);
        protected ToolStripMenuItem Settings = new ToolStripMenuItem(Language.Instance.Settings);
        protected ToolStripMenuItem About = new ToolStripMenuItem(Language.Instance.About);
        //Items
        protected ToolStripMenuItem OpenLog = new ToolStripMenuItem(Language.Instance.ViewLog);
        protected ToolStripMenuItem PerformUpdate = new ToolStripMenuItem(Language.Instance.PerformUpdate);
        protected ToolStripMenuItem Options = new ToolStripMenuItem(Language.Instance.Options);
        protected ToolStripMenuItem AboutUs = new ToolStripMenuItem(Language.Instance.About);
        //Homebutton
        protected ToolStripMenuItem Home = new ToolStripMenuItem(Language.Instance.Start) {Alignment = ToolStripItemAlignment.Right};
        public bool HomebuttonVisible
        {
            get { return Home.Available; }
            set { Home.Available = value; }
        }

        public StandardMenuStrip()
        {
            Dock = DockStyle.Fill;

            //Add File to MenuStrip
            Items.Add(File);
            Items.Add(Settings);
            Items.Add(About);
            Items.Add(Home);

            // FileMenu //
            {
                File.DropDownItems.Add(OpenLog);
                File.DropDownItems.Add(PerformUpdate);
            }
            // Settings //
            {
                Settings.DropDownItems.Add(Options);
            }
            // About //
            {
                About.DropDownItems.Add(PerformUpdate);
                About.DropDownItems.Add(AboutUs);
            }

            //Handlers
            {
                PerformUpdate.Click += new EventHandler(PerformUpdate_Click);
                Options.Click += new EventHandler(Options_Click);
                Home.Click += new EventHandler(Home_Click);
                OpenLog.Click += new EventHandler(OpenLog_Click);
                AboutUs.Click += new EventHandler(AboutUs_Click);
            }
        }

        void AboutUs_Click(object sender, EventArgs e)
        {
            new AboutDialog().ShowDialog();
        }

        void OpenLog_Click(object sender, EventArgs e)
        {
            Transition(new LogUserControl(), Panel.Main);
        }

        void Home_Click(object sender, EventArgs e)
        {
            Transition(new MainPanel(), Panel.Main, false);
            Transition(new StandardMenuStrip(), Panel.Menu);
        }

        void Options_Click(object sender, EventArgs e)
        {
            /*if (ProgressControl.HasInstance)
                if (MessageBox.Show("In order to change settings properly you need to close Mail-panel.", "Permission to close", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    ProgressControl.Dispose();*/
            //context.Transition(new Options(), false);
            new Options().ShowDialog();
        }

        protected virtual void PerformUpdate_Click(object sender, EventArgs e)
        {
            var dialog = new WaitDialog();

            dialog.Show();
            new Thread(delegate()
            {
                PMC.Interfaces.IUpdatable U = new Updater();
                try
                {
                    var update = U.PerformUpdate();
                    if (update != null)
                    {
                        if (
                            MessageBox.Show(
                                Language.Instance.UpdateAvailable, Language.Instance.UpdateFound, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(update.ToString());
                            PMC.Properties.Settings.Default.Save();
                        }
                    }
                    else
                        MessageBox.Show(Language.Instance.MostRecentVersion, Language.Instance.NoUpdateFound);
                }
                catch (ConnectionException)
                {
                    MessageBox.Show(Language.Instance.PleaseCheckConnection, Language.Instance.Connection, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //BetterDialog d = new BetterDialog("No valid connection found.", "Connection not valid","Please check your internetconnection and try again.", "Ok", "", "", SystemIcons.Exclamation);
                    //d.ShowDialog();
                }
                catch (Exception)
                {
                    MessageBox.Show(Language.Instance.ErrorOccured + " " + Language.Instance.PleaseTryAgain);
                }
                Invoke((MethodInvoker)dialog.Close);
            }
                ).Start();
        }

        #region Implementation of ITransitionable

        public void Transition(Control c, Panel panel, bool updateLayout = true)
        {
            var parent = Parent;
            while (parent != null)
            {
                if (parent is ITransitionable)
                {
                    (parent as ITransitionable).Transition(c, panel, updateLayout);
                    break;
                }
                parent = parent.Parent;
            }
        }

        #endregion
    }
}
